package com.sxwl.utils;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.util.StringUtils;

import java.util.Random;
import java.util.UUID;

/**
 * @author reading
 * @Description TODO
 * @Date 2020/3/8 12:59
 */
public class PasswordUtils {
    public static  String toMd5(String password,String salt){
        return DigestUtils.md5Hex(password+salt);
    }
    public  static  String salt(){
        return  StringUtils.replace(UUID.randomUUID().toString(),"-","");
    }
    public static void main(String[] args) {
        //salt-9f810d88ce1b43aeadef8a79d501c599
        //3fa8ad1fdc103726298005787376c91d
        String nowPassword="123456";
        String salt = salt();
        System.out.println("salt"+salt);
        String s = toMd5(nowPassword, salt);
        System.out.println("加密"+s);


    }
    /**
     * 生成指定位数的随机数字
     *
     * @param len
     * @return
     */
    public static String generateCode(int len) {
        len = Math.min(len, 8);
        int min = Double.valueOf(Math.pow(10, len - 1)).intValue();
        int num = new Random().nextInt(Double.valueOf(Math.pow(10, len + 1)).intValue() - 1) + min;
        return String.valueOf(num).substring(0, len);
    }
}
